﻿using System;
using System.Collections.Generic;
using System.Linq;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.SSO;
using OpenAuth.Repository.Domain;


namespace OpenAuth.App
{
    public class WorkNotePlanApp : BaseApp<WorkNotePlan>
    {
        public RevelanceManagerApp ReleManagerApp { get; set; }

        /// <summary>
        /// 加载列表
        /// </summary>
        public TableData Load(QueryWorkNotePlanListReq request)
        {
            return new TableData
            {
                count = Repository.GetCount(null),
                data = Repository.Find(request.page, request.limit, "Id desc")
            };
        }


        /// <summary>
        /// 分组显示所有人员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TableData LoadByGroup(QueryWorkNotePlanListReq request)
        {
            string cascadeId = ".0.";
            string title = request.Title;
            if (!string.IsNullOrEmpty(request.orgId))
            {
                var org = UnitWork.Find<Org>(u => u.Id == request.orgId).SingleOrDefault();
                cascadeId = org.CascadeId;
            }


            var orgList = UnitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId)).ToList();
            //var userIds = ReleManagerApp.Get(Define.USERORG, false, ids);


            List<WorkNotePlansOrg> workNotePlansOrgList = new List<WorkNotePlansOrg>();
            foreach (var item in orgList)
            {
                WorkNotePlansOrg workNotePlansOrg = new WorkNotePlansOrg();
                workNotePlansOrg.OrgName = item.Name;
                var ids = UnitWork.Find<Org>(u => u.CascadeId.Equals(item.CascadeId)).Select(u => u.Id).ToArray();
                var users = ReleManagerApp.Get(Define.USERORG, false, ids);
                List<WorkNotePlansView> workNotePlanslist = new List<WorkNotePlansView>();
                foreach (var userId in users)
                {
                    WorkNotePlansView workNotePlansView = new WorkNotePlansView();

                    //var workNotes = Repository.Find(u => u.UserId == userId&&u.CreateTime.ToString("yyyy-MM-dd")==request.CreateTime).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                    var workNotePlans = Repository.Find(u => u.UserId == userId && u.Title == title).OrderByDescending(u => u.Title).FirstOrDefault();
                    if (workNotePlans != null)
                    {
                        workNotePlansView.Title = workNotePlans.Title;
                        workNotePlansView.Content = workNotePlans.Content;
                        workNotePlansView.UserId = workNotePlans.UserId;
                        workNotePlansView.UserName = UnitWork.Find<User>(u => u.Id == workNotePlans.UserId).FirstOrDefault().Name;
                        workNotePlansView.CreateDate = workNotePlans.CreateTime.ToString("yyyy-MM-dd");
                        workNotePlansView.Results = workNotePlans.Results;
                        workNotePlanslist.Add(workNotePlansView);
                    }
                    else
                    {
                        int userCount = UnitWork.Find<User>(u => u.Id == userId).Count();
                        if (userCount > 0)
                        {
                            workNotePlansView.Content = "";
                            workNotePlansView.UserId = userId;
                            workNotePlansView.UserName = UnitWork.Find<User>(u => u.Id == userId).FirstOrDefault().Name;
                            workNotePlansView.CreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                            workNotePlansView.Title = "";
                            workNotePlansView.Results = 0;
                            workNotePlanslist.Add(workNotePlansView);
                        }
                    }

                }
                workNotePlansOrg.data = workNotePlanslist;
                workNotePlansOrgList.Add(workNotePlansOrg);
            }

            TableData tableData = new TableData();
            tableData.code = 1;
            tableData.data = workNotePlansOrgList;

            return tableData;
        }

        public void Add(WorkNotePlan obj)
        {
            Repository.Add(obj);
        }

        public void Update(WorkNotePlan obj)
        {
            UnitWork.Update<WorkNotePlan>(u => u.Id == obj.Id, u => new WorkNotePlan
            {
                //todo:要修改的字段赋值
            });

        }

        public void AddOrUpdate(WorkNotePlan obj)
        {
            int wcount = Repository.Find(w => w.Title == obj.Title && w.UserId == obj.UserId).Count();
            if (wcount == 0)
            {
                Repository.Add(obj);
            }
            else
            {
                UnitWork.Update<WorkNotePlan>(u => u.Title == obj.Title && u.UserId == obj.UserId, u => new WorkNotePlan
                {
                    Title = obj.Title,
                    Content = obj.Content,
                    CreateTime = obj.CreateTime,
                    UpdateTime = DateTime.Now,
                    Remark = obj.Remark,
                    Results = obj.Results

                });
            }

        }

        public TableData LoadlistSum(QueryWorkNotePlanListReq request)
        {
            string cascadeId = ".0.";
            DateTime createTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            if (!string.IsNullOrEmpty(request.orgId))
            {
                var org = UnitWork.Find<Org>(u => u.Id == request.orgId).SingleOrDefault();
                cascadeId = org.CascadeId;
            }


            var orgList = UnitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId)).ToList();
            //var userIds = ReleManagerApp.Get(Define.USERORG, false, ids);


            List<WorkNotePlansOrg> workNotePlansOrgList = new List<WorkNotePlansOrg>();




        
            List<WorkNotePlansView> workNotePlanslist = new List<WorkNotePlansView>();

            foreach (var item in orgList)
            {
                WorkNotePlansOrg workNotePlansOrg = new WorkNotePlansOrg();
                workNotePlansOrg.OrgName = item.Name;
                var ids = UnitWork.Find<Org>(u => u.CascadeId.Equals(item.CascadeId)).Select(u => u.Id).ToArray();
                var users = ReleManagerApp.Get(Define.USERORG, false, ids);

                foreach (var userId in users)
                {
                    WorkNotePlansView workNotePlansView = new WorkNotePlansView();

                    var workNotePlans = Repository.Find(u => u.UserId == userId && u.Title == request.Title).OrderByDescending(u => u.CreateTime).FirstOrDefault();
                    if (workNotePlans != null)
                    {
                        workNotePlansView.Content = workNotePlans.Content;
                        workNotePlansView.UserId = workNotePlans.UserId;
                        workNotePlansView.UserName = UnitWork.Find<User>(u => u.Id == workNotePlans.UserId).FirstOrDefault().Name;
                        workNotePlansView.CreateDate = workNotePlans.CreateTime.ToString("yyyy-MM-dd");
                        workNotePlansView.Title = workNotePlans.Title;
                        workNotePlansView.Results = workNotePlans.Results;
                        workNotePlansView.Remark = workNotePlans.Remark;
                        workNotePlanslist.Add(workNotePlansView);
                    }
                    else
                    {
                        int userCount = UnitWork.Find<User>(u => u.Id == userId).Count();
                        if (userCount > 0)
                        {
                            workNotePlansView.Content = "";
                            workNotePlansView.UserId = userId;
                            workNotePlansView.UserName = UnitWork.Find<User>(u => u.Id == userId).FirstOrDefault().Name;
                            workNotePlansView.CreateDate = createTime.ToString("yyyy-MM-dd");
                            workNotePlansView.Title = "";
                            workNotePlansView.Results = 0;
                            workNotePlansView.Remark = "";
                            workNotePlanslist.Add(workNotePlansView);
                        }
                    }
                }
                workNotePlansOrg.data = workNotePlanslist;
                workNotePlansOrgList.Add(workNotePlansOrg);
            }


            TableData tableData = new TableData();
            tableData.code = 1;
            tableData.data = workNotePlansOrgList;

            return tableData;
        }

    }
}